﻿using MiniFox.Components;
using MiniFox.Data.EntityFramework;
using MiniFox.Infrastructure;
using Sys.Core.Models.Queries;
using Sys.Core.Models.VO;
using Sys.Core.Providers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sys.Data
{
    [AutoResolve]
    public class DispatcherProvider : EntityDataProvider<PlatformDbContext>, IDispatcherProvider
    {
        public MailFailItemVo FailItem(string messageId)
        {
            var item = this.CurrentDbContext.MailSentFail.SingleOrDefault(m => m.MessageId == messageId);

            return new MailFailItemVo
            {
                MessageId = item.MessageId,
                AccountName = item.AccountName,
                Sender = item.Sender,
                Receiver = item.Receiver,
                CCList = item.CcList,
                BCCList = item.BccList,
                Title = item.Title,
                TemplateName = item.TemplateName,
                Data = item.SendData,
                Body = item.Body,
                SendTime = item.SendTime,
                Exception = item.Exception
            };
        }

        public ResultObject<IEnumerable<MailFailItemVo>> FailQuery(PagedQueryObject<MailQuery> request)
        {
            var result = this.PagedQueryExecute<MailSentFail>(request);
            return result.GetResultObject(item => new MailFailItemVo
            {
                MessageId = item.MessageId,
                AccountName = item.AccountName,
                Sender = item.Sender,
                Receiver = item.Receiver,
                CCList = item.CcList,
                BCCList = item.BccList,
                Title = item.Title,
                TemplateName = item.TemplateName,
                //Data = item.Data,
                //Body = item.Body,
                SendTime = item.SendTime,
                //Exception = item.Exception
            });
        }

        public MailSentItemVo SentItem(string messageId)
        {
            var item = this.CurrentDbContext.MailSent.SingleOrDefault(m => m.MessageId == messageId);

            return new MailSentItemVo
            {
                MessageId = item.MessageId,
                AccountName = item.AccountName,
                Sender = item.Sender,
                Receiver = item.Receiver,
                CCList = item.CcList,
                BCCList = item.BccList,
                Title = item.Title,
                TemplateName = item.TemplateName,
                Data = item.SendData,
                Body = item.Body,
                SendTime = item.SendTime,
                Attachments = (from a in this.CurrentDbContext.File 
                               join m in this.CurrentDbContext.MessageAttachment on a.FileId equals m.FileId
                               where m.MessageId == messageId 
                               select new { a.FileId, a.FileName }).ToArray()
            };
        }

        public ResultObject<IEnumerable<MailSentItemVo>> SentQuery(PagedQueryObject<MailQuery> request)
        {
            var result = this.PagedQueryExecute<MailSent>(request);
            return result.GetResultObject(item => new MailSentItemVo
            {
                MessageId = item.MessageId,
                AccountName = item.AccountName,
                Sender = item.Sender,
                Receiver = item.Receiver,
                CCList = item.CcList,
                BCCList = item.BccList,
                Title = item.Title,
                TemplateName = item.TemplateName,
                //Data = item.Data,
                //Body = item.Body,
                SendTime = item.SendTime,
                Attachments = (from a in this.CurrentDbContext.File
                               join m in this.CurrentDbContext.MessageAttachment on a.FileId equals m.FileId
                               where m.MessageId == item.MessageId
                               select new { a.FileId, a.FileName }).ToArray()
            });
        }

        public MessageVo MessageItem(string messageId)
        {
            var item = this.CurrentDbContext.MessageSent.SingleOrDefault(m => m.MessageId == messageId);

            return new MessageVo
            {
                MessageId = item.MessageId,
                GroupName = item.GroupName,
                SenderId = item.SenderId,
                SenderName = item.SenderName,
                ReceiverId = item.ReceiverId,
                ReceiverName = item.ReceiverName,
                Title = item.Title,
                TemplateName = item.TemplateName,
                Data = item.Data,
                Body = item.Body,
                SendTime = item.SendTime
            };
        }

        public ResultObject<IEnumerable<MessageVo>> MessageQuery(PagedQueryObject<MessageQuery> request)
        {
            var result = this.PagedQueryExecute<MessageSent>(request);
            return result.GetResultObject(item => new MessageVo
            {
                MessageId = item.MessageId,
                GroupName = item.GroupName,
                SenderId = item.SenderId,
                SenderName = item.SenderName,
                ReceiverId = item.ReceiverId,
                ReceiverName = item.ReceiverName,
                Title = item.Title,
                TemplateName = item.TemplateName,
                Data = item.Data,
                Body = item.Body,
                SendTime = item.SendTime
            });
        }

    }
}
